﻿using Microsoft.AspNetCore.Http;
using MiniFox.Components;
using MiniFox.Infrastructure;
using MiniFox.Platform.Models;
using MiniFox.Platform.State;
using MiniFox.Resources;
using MiniFox.Utility;
using MiniFox.Web;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace MiniFox.Platform.Web
{
    /// <summary>
    /// 
    /// </summary>
    public static class HttpExtensions
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="httpContext"></param>
        /// <param name="stateManagement"></param>
        public static void InitStateManagement(this HttpContext httpContext, IClientStateManagement stateManagement)
        {
            var httpRequest = httpContext.Request;
            stateManagement.FullApplicationPath = httpRequest.GetFullApplicationPath();
            stateManagement.FullHost = httpRequest.GetFullHost();
            stateManagement.ClientInfomation.ClientIP = httpRequest.GetClientIP();
            stateManagement.ClientInfomation.UserAgent = httpRequest.Headers.UserAgent;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="httpContext"></param>
        /// <returns></returns>
        public static IClientStateManagement GetClientState(this HttpContext httpContext)
        {
            var stateManagement = httpContext.Items[ClientStateManagement.CLIENT_STATE_CONTEXT] as IClientStateManagement;
            if (stateManagement == null)
            {
                stateManagement = ComponentFactory.GetComponent<IClientStateManagement>();
                httpContext.InitStateManagement(stateManagement);

                httpContext.Items[ClientStateManagement.CLIENT_STATE_CONTEXT] = stateManagement;
            }
            return stateManagement;
        }
    }
}
